<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicModal v-bind="$attrs" @register="registerModal" @ok="handleSubmit">
      <template #title>
        <span> {{ getTitle.value }} </span>
      </template>
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewSportsSportsWeightAdditionalModal',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import {
    SportsIndexBonusRules,
    sportsIndexBonusRulesForm,
    sportsIndexBonusRulesSave,
  } from '/@/api/sports/sportsIndexBonusRules';
  const emit = defineEmits(['success', 'register']);
  const { t } = useI18n('sports.sportsWeight');
  const { showMessage } = useMessage();
  const getTitle = computed(() => ({
    value: record.value.isNewRecord ? t('新增附加分') : t('编辑附加分'),
  }));
  const record = ref<SportsIndexBonusRules>({} as SportsIndexBonusRules);
  const inputFormSchemas: FormSchema[] = [
    {
      label: t('指标名称'),
      field: 'sportsWeight.indexName',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
      colProps: { lg: 24, md: 24 },
      required: true,
      dynamicDisabled: ({ values }) => {
        return !values.field7;
      },
    },
    {
      label: t('适配年级'),
      field: 'adaptGrade',
      component: 'CheckboxGroup',
      componentProps: {
        dictType: 'vke_grade',
        allowClear: true,
      },
      colProps: { lg: 24, md: 24 },
      required: true,
    },
    {
      label: t('性别'),
      field: 'adaptSex',
      component: 'Select',
      componentProps: {
        dictType: 'sys_user_sex',
      },
      colProps: { lg: 24, md: 24 },
      required: true,
    },
    {
      label: t('测评值'),
      field: 'score',
      component: 'Input',
      componentProps: {
        dictType: 'vke_grade',
        allowClear: true,
      },
      colProps: { lg: 24, md: 24 },
      required: true,
    },
    {
      label: t('附加分'),
      field: 'bonusPoints',
      component: 'Input',
      componentProps: {
        dictType: 'vke_grade',
        allowClear: true,
      },
      colProps: { lg: 24, md: 24 },
      required: true,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });
  let dataIndex;
  const [registerModal, {closeModal, setModalProps }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ loading: true });
    dataIndex = data;
    const res = await sportsIndexBonusRulesForm(data);
    record.value = (res.sportsIndexBonusRules || {}) as SportsIndexBonusRules;
    if (record.value.sportsWeight.indexName == undefined) {
      record.value.sportsWeight.indexName = data.indexName;
    }
    setFieldsValue(record.value);
    setModalProps({ loading: false });
  });
  async function handleSubmit() {
    try {
      let data = await validate();
      delete data.sportsWeight;
      data.indexId = dataIndex.indexId;
      data.bonusRulesId = dataIndex.bonusRulesId;
      let res = await sportsIndexBonusRulesSave(data);
      showMessage(res.message);
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
    }
  }
</script>
